package com.cskaoyan.service;

import com.cskaoyan.bean.bo.AdminOrderListBo;
import com.cskaoyan.bean.bo.AdminOrderRefundBo;
import com.cskaoyan.bean.bo.AdminOrderReplyBO;
import com.cskaoyan.bean.bo.AdminOrderShipBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.AdminOrderChannelDateEntityVo;
import com.cskaoyan.bean.vo.AdminOrderChannelVo;
import com.cskaoyan.bean.vo.AdminOrderShipVo;
import com.cskaoyan.bean.vo.WXUserIndexVo;
import com.cskaoyan.bean.vo.admin_order_detail.*;
import com.cskaoyan.bean.vo.admin_order_list.AdminOrderListDataEntityListEntityVo;
import com.cskaoyan.bean.vo.admin_order_list.AdminOrderListDataEntityVo;
import com.cskaoyan.bean.vo.admin_order_list.AdminOrderListVo;
import com.cskaoyan.bean.vo.admin_order_list.AdminOrderListVoNew;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Trellis
 * @since 2022/09/06 19:09
 */
@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketGoodsSpecificationMapper specificationMapper;
    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Override
    public BaseRespVo getListByBo(AdminOrderListBo listBo) {
        //进行分页
        PageHelper.startPage(listBo.getPage(), listBo.getLimit());
        MarketOrderExample example = new MarketOrderExample();
        example.setDistinct(true);
        String clause = listBo.getSort()+" "+listBo.getOrder();
        example.setOrderByClause(clause);
        //只回显deleted为false的
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        if (listBo.getOrderStatusArray() != null && !"".equals(listBo.getOrderStatusArray())) {
            criteria.andOrderStatusEqualTo(listBo.getOrderStatusArray());
        }
        if (listBo.getOrderSn() != null && !"".equals(listBo.getOrderSn())) {
            criteria.andOrderSnEqualTo(listBo.getOrderSn());
        }
        if (listBo.getOrderId() != null && !"".equals(listBo.getOrderId())) {
            criteria.andIdEqualTo(listBo.getOrderId());
        }
        if (listBo.getUserId() != null && !"".equals(listBo.getUserId())) {
            criteria.andUserIdEqualTo(listBo.getUserId());
        }
        //时间判断
        // if (listBo.getStart() != null && !"".equals(listBo.getStart())) {
        //     criteria.andAddTimeGreaterThan(listBo.getStart());
        // }
        // if (listBo.getStart() != null && !"".equals(listBo.getStart())) {
        //     criteria.andEndTimeLessThan(listBo.getEnd());
        // }


        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(example);
        AdminOrderListVoNew adminOrderListVoNew = new AdminOrderListVoNew();
        for (MarketOrder marketOrder : marketOrders) {
            adminOrderListVoNew.copyList(marketOrder);
        }

        PageInfo pageInfo = new PageInfo(marketOrders);
        int total =(int) pageInfo.getTotal();

        adminOrderListVoNew.setLimit(listBo.getLimit());

        adminOrderListVoNew.setPage(listBo.getPage());
        adminOrderListVoNew.setTotal(total);
        adminOrderListVoNew.setPages(pageInfo.getPages());

        PageHelper.clearPage();

        BaseRespVo baseRespVo = BaseRespVo.ok(adminOrderListVoNew);
        return baseRespVo;
    }

    @Override
    public BaseRespVo getDetailById(Integer id) {
        //order的Id
        NewAdminOrderDetailVo newAdminOrderDetailVo = new NewAdminOrderDetailVo();
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(id);
        newAdminOrderDetailVo.copyOrder(marketOrder);


        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        example.createCriteria().andOrderIdEqualTo(id);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(example);
        for (MarketOrderGoods marketOrderGood : marketOrderGoods) {
            newAdminOrderDetailVo.copyOrderGoods(marketOrderGood);
        }

        Integer userId = marketOrder.getUserId();
        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(userId);
        newAdminOrderDetailVo.copyUser(marketUser);

        BaseRespVo baseRespVo = BaseRespVo.ok(newAdminOrderDetailVo);
        return baseRespVo;


    }

    @Override
    public AdminOrderShipVo updateStatusByOrderId(AdminOrderShipBo shipBo, Integer id, Short status) {
        Date date = new Date(System.currentTimeMillis());
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(id);
        marketOrder.setOrderStatus(status);
        marketOrder.setShipTime(date);
        BeanUtils.copyProperties(shipBo,marketOrder);
        int key = marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        AdminOrderShipVo shipVo = null;
        if (key == 1) {
            shipVo = new AdminOrderShipVo(0, "成功");
        }
        return shipVo;
    }

    @Override
    public WXUserIndexVo getUserIndex() {

        int unpaid = getOrderStatusNum((short) 101);
        int unship = getOrderStatusNum((short) 201);
        int unrecv = getOrderStatusNum((short) 301);
        int uncomment = getOrderStatusNum((short) 401);

        WXUserIndexVo indexVo = new WXUserIndexVo();
        indexVo.setOrderEntity(unrecv,uncomment,unpaid,unship);
        return indexVo;
    }

    @Override

    public AdminOrderChannelVo getChannel() {
        AdminOrderChannelVo adminOrderChannelVo = new AdminOrderChannelVo();
        List<AdminOrderChannelDateEntityVo> data = adminOrderChannelVo.getData();
        AdminOrderChannelDateEntityVo dateEntityVo = new AdminOrderChannelDateEntityVo("ZTO","中通快递");
        AdminOrderChannelDateEntityVo dateEntityVo1 = new AdminOrderChannelDateEntityVo("YTO","圆通快递");
        AdminOrderChannelDateEntityVo dateEntityVo2 = new AdminOrderChannelDateEntityVo("YD","韵达快递");
        AdminOrderChannelDateEntityVo dateEntityVo3 = new AdminOrderChannelDateEntityVo("YZPY","邮政快递包裹");
        AdminOrderChannelDateEntityVo dateEntityVo4 = new AdminOrderChannelDateEntityVo("EMS","EMS");
        AdminOrderChannelDateEntityVo dateEntityVo5 = new AdminOrderChannelDateEntityVo("DBL","德邦快递");
        AdminOrderChannelDateEntityVo dateEntityVo6 = new AdminOrderChannelDateEntityVo("FAST","快捷快递");
        AdminOrderChannelDateEntityVo dateEntityVo7 = new AdminOrderChannelDateEntityVo("ZJS","宅急送");
        AdminOrderChannelDateEntityVo dateEntityVo8 = new AdminOrderChannelDateEntityVo("TNT","TNT快递");
        AdminOrderChannelDateEntityVo dateEntityVo9 = new AdminOrderChannelDateEntityVo("UPS","UPS");
        AdminOrderChannelDateEntityVo dateEntityVo10 = new AdminOrderChannelDateEntityVo("DHL","DHL");
        AdminOrderChannelDateEntityVo dateEntityVo11 = new AdminOrderChannelDateEntityVo("FEDEX","FEDEX德邦（国内件）");
        AdminOrderChannelDateEntityVo dateEntityVo12 = new AdminOrderChannelDateEntityVo("FEDEX_GJ","FEDEX德邦（国际件）");

        data.add(dateEntityVo);
        data.add(dateEntityVo1);
        data.add(dateEntityVo2);
        data.add(dateEntityVo3);
        data.add(dateEntityVo4);
        data.add(dateEntityVo5);
        data.add(dateEntityVo6);
        data.add(dateEntityVo7);
        data.add(dateEntityVo8);
        data.add(dateEntityVo9);
        data.add(dateEntityVo10);
        data.add(dateEntityVo11);
        data.add(dateEntityVo12);
        adminOrderChannelVo.setErrmsg("成功");
        adminOrderChannelVo.setErrno(0);
        return adminOrderChannelVo;
    }
    public int reply(AdminOrderReplyBO adminOrderReplyBO) {
        MarketComment marketComment = new MarketComment();
        marketComment.setId(adminOrderReplyBO.getCommentId());
        marketComment.setAdminContent(adminOrderReplyBO.getContent());
        marketComment.setUpdateTime(new Date(System.currentTimeMillis()));
        //先查有没有回复
        MarketComment oldComment = marketCommentMapper.selectByPrimaryKey(adminOrderReplyBO.getCommentId());
        if (!StringUtil.isEmpty(oldComment.getAdminContent())) {
            //不空已经回复
            return 622;
        }
        //进行更改
        marketCommentMapper.updateByPrimaryKeySelective(marketComment);
        return 0;

    }

    @Override
    public void deleteOrder(Integer orderId) {
        MarketOrder record = new MarketOrder();
        record.setId(orderId);
        record.setDeleted(true);
        marketOrderMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public BaseRespVo updateStatusByBo(AdminOrderRefundBo refundBo, Short status, int orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setOrderStatus(status);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        BaseRespVo baseRespVo = BaseRespVo.ok();
        return baseRespVo;
    }

    //辅助方法
    private Integer getOrderStatusNum(short orderStatus) {
        MarketOrderExample orderExample = new MarketOrderExample();
        Session session = SecurityUtils.getSubject().getSession();
        MarketUser info = (MarketUser) session.getAttribute("info");
        orderExample.createCriteria().andOrderStatusEqualTo(orderStatus).andDeletedEqualTo(false)
                .andUserIdEqualTo(info.getId());

        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(orderExample);
        return marketOrders.size();
    }
}
